package com.qxsx.midi.web;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.qxsx.midi.R;

/**
 * Created by sumile on 2015/8/28.
 * Blog:<a href="http://sumile.cn">sumile</a>
 * ┃　　　┃ 神兽保佑　 ┃　　　┃
 * ┃　　　┃ 代码无BUG！┃　　　┃
 */
public class SWebChromeClient extends WebChromeClient {
    private View xprogressvideo;
    private Context context;
    private View xCustomView;
    private CustomViewCallback xCustomViewCallback;
    private Activity activity;
    /**
     * 没有全屏播放时的webview
     */
    private WebView webView;
    private FrameLayout video_fullView;// 全屏时视频加载view

    public SWebChromeClient(Context context, final WebView webView, FrameLayout video_fullView) {
        this.context = context;
        this.webView = webView;
        if (context instanceof Activity) {
            this.activity = (Activity) context;
        }
        this.video_fullView = video_fullView;
    }

    public SWebChromeClient(Context context, final WebView webView, final Activity activity, FrameLayout video_fullView) {
        this.context = context;
        this.webView = webView;
        this.activity = activity;
        this.video_fullView = video_fullView;
    }

    // 播放网络视频时全屏会被调用的方法
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        webView.setVisibility(View.INVISIBLE);
        // 如果一个视图已经存在，那么立刻终止并新建一个
        if (xCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        video_fullView.addView(view);
        xCustomView = view;
        xCustomViewCallback = callback;
        video_fullView.setVisibility(View.VISIBLE);
    }

    // 视频播放退出全屏会被调用的
    @Override
    public void onHideCustomView() {
        if (xCustomView == null)// 不是全屏播放状态
            return;

        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        xCustomView.setVisibility(View.GONE);
        video_fullView.removeView(xCustomView);
        xCustomView = null;
        video_fullView.setVisibility(View.GONE);
        xCustomViewCallback.onCustomViewHidden();
        webView.setVisibility(View.VISIBLE);
    }

    // 视频加载时进程loading
    @Override
    public View getVideoLoadingProgressView() {
        if (xprogressvideo == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            xprogressvideo = inflater.inflate(R.layout.video_loading_progress, null);
        }
        return xprogressvideo;
    }

    /**
     * 判断是否是全屏
     *
     * @return
     */
    public boolean inCustomView() {
        return (xCustomView != null);
    }

    /**
     * 全屏时按返加键执行退出全屏方法
     */
    public void hideCustomView() {
        onHideCustomView();
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}
